package com.ys.service.sys.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bo.enume.sys.StatusEnum;
import com.ys.entity.sys.YsDict;
import com.ys.entity.sys.YsModalSelector;
import com.ys.entity.sys.YsModalSelectorField;
import com.ys.mapper.sys.YsModalSelectorMapper;
import com.ys.service.sys.YsModalSelectorService;
import com.ys.util.admin.GridSelectorModalUtil;
import com.ys.vo.R;

@Service("ysModalSelectorService")
public class YsModalSelectorServiceImpl implements YsModalSelectorService {
	
	@Autowired
	private YsModalSelectorMapper ysModalSelectorMapper;

	@Override
	public R add(String name, String nameTable, String pathGridRequest) {
		
		YsModalSelector entity = YsModalSelector.ysModalSelector(name, nameTable, pathGridRequest, StatusEnum.ON_USED.value);
		
		ysModalSelectorMapper.insert(entity);
		
		return R.ok();
	}

	@Override
	public R edit(Long id, String name, String nameTable, String pathGridRequest) {
		
		YsModalSelector entity = new YsModalSelector().setId(id).queryFirst();
		entity
			.setName(name)
			.setNameTable(nameTable)
			.setPathGridRequest(pathGridRequest);
		
		ysModalSelectorMapper.update(entity);
		
		return R.ok();
	}

	@Override
	@Transactional
	public R deleteTrue(Long id) {
		if (id == null) {
			return R.error("ID不能为空");
		}
		
		ysModalSelectorMapper.deleteTrue(id);
		
		return R.ok();
	}

	@Override
	public R modalConfig(String modalKey, Integer multySelected) {
		
		multySelected = multySelected == null ? 0 : multySelected;
		
		if (modalKey == null || modalKey.length() == 0) {
			return R.error("弹出框识别码不能为空");
		}

		
		YsModalSelector ysModalSelector = new YsModalSelector().setNameTable(modalKey).queryFirst();
		if (ysModalSelector == null) {
			return R.error("弹出框不存在");
		}
		
		String pathGridRequest = ysModalSelector.getPathGridRequest();
		
		List<YsModalSelectorField> listYsModalSelectorField = new YsModalSelectorField()
				.setPid(ysModalSelector.getId())
				.setOrderby("A.SortNo asc")
				.queryList();
		
		// dictKey-List<SysDict>的MAP
		Map<String, List<YsDict>> KEY_LISTDICT_MAP = new HashMap<String, List<YsDict>>();
		List<YsDict> listYsDict = new YsDict()
				.where("A.DictKey in (select DictKeyBase from YS_ModalSelectorField where Pid = " + ysModalSelector.getId() + ")")
				.setOrderby("A.DictKey asc,A.SortNo asc")
				.queryList();
		for(YsDict ysDict : listYsDict) {
			String dictKey = ysDict.getDictKey();
			if (KEY_LISTDICT_MAP.containsKey(dictKey)) {
				KEY_LISTDICT_MAP.get(dictKey).add(ysDict);
			} else {
				List<YsDict> listValue = new ArrayList<YsDict>();
				listValue.add(ysDict);
				KEY_LISTDICT_MAP.put(dictKey, listValue);
			}
		}
		List<ModalConfigDto.ColGridDto> colGrids = GridSelectorModalUtil.listColumn(listYsModalSelectorField, multySelected, KEY_LISTDICT_MAP);
		
		List<ModalConfigDto.ConditionDto> conditions = GridSelectorModalUtil.listCondition(listYsModalSelectorField, KEY_LISTDICT_MAP);
		ModalConfigDto dto = new ModalConfigDto(multySelected, pathGridRequest, colGrids, conditions);
		return R.ok(dto);
	}
}
